@extends('layout.app') @section('content')

    <div class="box-body">
        <table id="rowed3"></table>
        <div id="prowed3"></div>
    </div>
    <script type="text/javascript" src="/js/laydate/laydate.js">
    </script>
    <script>

        $(document).ready(function() {
            var data = {
                url: "/redpackets",
                colNames: ["ID", "红包倒计时", "发红包时间", "红包总金额(单位:元)", "红包个数", "红包类型", "实际发放金额(单位:元)", "实际发放个数", "最小金额(单位:分)", "is_run", "是否结束"],
                colModel: [{
                    name: 'id',
                    sortable: false,
                    key: true
                }, {
                    name: "count_down",
                    formatter:'date',
                    editable: true,
                    formatoptions: {srcformat: 'Y-m-d H:i:s', newformat: 'Y-m-d H:i:s'},
                    searchoptions: {
                        sopt: ['lt', 'le', 'gt', 'ge'],
                        dataInit : function (elem) {
                            $(elem).datepicker({
                                dateFormat: 'yy-mm-dd',
                                monthNames: ['一月','二月','三月','四月','五月','六月','七月','八月','九月','十月','十一月','十二月'],
                                dayNamesMin: ['日','一','二','三','四','五','六'] });
                        }
                    },
                    editoptions: {
                        dataInit : function (elem) {
                            laydate.render({
                                elem: elem //指定元素
                            });
                        }
                    },

                }, {
                    name: 'begin_time',
                    formatter:'date',
                    editable: true,
                    formatoptions: {srcformat: 'Y-m-d H:i:s', newformat: 'Y-m-d H:i:s'},
                    searchoptions: {
                        sopt: ['lt', 'le', 'gt', 'ge'],
                        dataInit : function (elem) {
                            $(elem).datepicker({
                                dateFormat: 'yy-mm-dd',
                                monthNames: ['一月','二月','三月','四月','五月','六月','七月','八月','九月','十月','十一月','十二月'],
                                dayNamesMin: ['日','一','二','三','四','五','六'] });
                        }
                    },
                    editoptions: {
                        dataInit : function (elem) {
                            laydate.render({
                                elem: elem //指定元素
                            });
                        }
                    },
                },{
                    name: 'total_amount',
                    editable: true,
                    editrules: {
                        required: true,
                        integer: true
                    },
                    formatter: function(cellvalue) {
                        return cellvalue / 100;
                    }
                }, {
                    name: 'num',
                    editable: true,
                    editrules: {
                        required: true,
                        integer: true
                    }
                }, {
                    name: 'type',
                    editable: true,
                    edittype: 'select',
                    editoptions: {
                        value: "{{$rs}}"
                    },
                    formatter: function(cellvalue, options, object) {
                        if(cellvalue == 1){
                            return '默认随机红包';
                        }
                    }
                }, {
                    name: 'send_amount',
                    editable: false,
                    formatter: function(cellvalue) {
                        return cellvalue / 100;
                    }
                }, {
                    name: 'send_num',
                    editable: false,
                }, {
                    name: 'min_money',
                    editable: true,
                    editrules: {
                        required: true,
                        integer: true
                    },
                    formatter: function(cellvalue) {
                        return cellvalue;
                    }
                }, {
                    name: 'is_run',
                    sortable: false,
                    search : false,
                    hidden:true,
                }, {
                    name: 'status',
                    sortable: false,
                    search : false,
                }],
                caption: '<i class="fa fa-table"></i> {{$label2}}',
                editurl: "/redpackets/edit",
                sortname: "id",
                sortorder: "desc",
                height:'auto',
                autowidth: true,
                gridComplete : function() {
                    var ids = jQuery("#rowed3").jqGrid('getDataIDs');

                    for ( var i = 0; i < ids.length; i++) {
                        if(ids[i]){
                            var cl = ids[i];
                        }else{
                            break;
                        }

                        var rowData = $("#rowed3").jqGrid("getRowData",cl);
                        var run = rowData.is_run;

                        if (parseInt(run) == 1){
                            de = '<a href="javascript:;" onclick="closePacket('+cl+')" data-toggle="modal" data-target="#closeModal"><span class="text-info">开放</span></a>';
                        }else{
                            de = '结束';
                        }

                        jQuery("#rowed3").jqGrid('setRowData', ids[i],{
                            status : de
                        });
                    }

                    $(this).footerData("set", {
                        "total_amount" : '<?php echo $sum1?>',
                        "send_amount"  : '<?php echo $sum2?>'
                    });
                }
            };
            jqGrid(data, true, true, true);

        });

        var loader = "<div style='text-align:center'><img src='/img/loaders/15.gif'></div>";

        function closePacket(id) {
            $("#close_id").next().remove();
            $("#close-button").attr("disabled",false);
            $("#close-cancel").attr("disabled",false);
            $("#closeSpan").html("确定结束本场红包设置吗?");
            $("#closeModal").modal([]);
            $("#close_id").val(id);
        }
        function closeSubmit(id) {
            $("#close-button").attr("disabled","disabled");
            $("#close-cancel").attr("disabled","disabled");
            $("#close_id").after(loader);
            $.post("/redpackets/closepacket",{id: id}, function(result) {
                console.log(result);
                if (result.success) {
                    $("#rowed3").trigger("reloadGrid");
                    $.gritter.add({
                        text:'已结束本场红包活动',
                        sticky: false,
                        time: '',
                        class_name: 'gritter-light'});

                } else {
                    $.gritter.add({
                        title: '错误信息',
                        text:'操作失败',
                        sticky: false,
                        time: ''});
                }
                $("#closeModal").modal('hide');
            });
        }
    </script>

    <div class="modal fade" id="closeModal" tabindex="-1" role="dialog" data-backdrop="static" aria-labelledby="closeModalLabel">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-body">
                    <h4 class="modal-title"><span id="closeSpan" class="text-danger"></span></h4>
                    <input type="hidden" id="close_id"/>
                </div>
                <div class="modal-footer">
                    <button id="close-button" type="submit" class="btn btn-success" onclick="closeSubmit($('#close_id').val())"><i class="fa fa-check"></i> 确定</button>
                    <button id="close-cancel" type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                </div>
            </div>
        </div>
    </div>
@endsection